# Created by 黄景涛
# DATE 2024/7/24
import requests

from user_manage_login import login
from org_settings import base_url
from org_settings import fk
import math
import yaml

# 请求头配置
headers = {
    "User-Agent": fk.user_agent(),
    "Cookie": "",
    "X-Csrftoken": ""
}


def update_headers():
    cookies, token = login()
    # 更新headers中Cookie键值对, X-Csrftoken
    headers['Cookie'] = cookies
    headers['X-Csrftoken'] = token


update_headers()

# 域名
base_url = base_url

# full_url      /o/bk_user_manage/api/v1/web/profiles/229326/
full_update_url = base_url + '/o/bk_user_manage/api/v1/web/profiles'

# 全局的session对象
session = requests.session()


# 更新用户信息
def update_profile(user_id, org_id):
    full_url = full_update_url + f'/{user_id}/'
    data = {
        "leader": [],
        "departments": [
            org_id
        ],
        "password_valid_days": -1,
        "display_name": "陈荣",
        "email": "chenrong@qq.com",
        "telephone": "13937858022",
        "iso_code": "CN",
        # 账户状态，status_choice = ("NORMAL", "LOCKED", "DELETED", "DISABLED", "EXPIRED")
        # 在职状态，staff_status_choice = ("IN", "OUT")
        "staff_status": "OUT",
        "status": "EXPIRED",
        "position": "0",
        "wx_userid": "weixin7mq6Y8kWie",
        "qq": "",
        "account_expiration_date": "2023-01-01",
        "sex": 0,
        "customstr": "",
        "customenumsimple": 0,
        "customenummultipart": 0,
        "customnums": "",
        "customdatetime": "",
        "orgname": "",
        "orgid": ""
    }

    try:
        resp = session.patch(full_url, json=data, headers=headers)
        result = resp.json().get('result')
    except:
        raise Exception('用户更新的接口访问失败！')

    if result:
        print(f'用户【{user_id}】更新成功！')
    else:
        raise Exception(f'用户【{user_id}】更新失败！')



if __name__ == '__main__':
    # 229327
    user_id = "229327"
    org_id = "30064"
    update_profile(user_id, org_id)
